﻿// ===============================================================================
// XCI.Parent 西安交通信息WinForm开发框架 2019 版权所有
// ===============================================================================

using DevExpress.XtraLayout.Utils;
using System;
using System.Windows.Forms;
using DevExpress.Images;
using XCI.Extensions;
using XCI.Helper;
using XCI.Sys.Model;
using XCI.Sys.Service;
using XCI.Windows;
using XCI.Windows.ImagePicker;

namespace XCI.Sys.Forms
{
    public partial class FrmModuleEdit : BaseForm
    {
        private FrmImagePicker _frmImagePicker;
        public FrmModuleEdit(EditFormArgs args)
        {
            InitializeComponent();
            layoutControl.Args = args;
        }

        private async void Form_Load(object sender, EventArgs e)
        {
            await layoutControl.BindingModelAsync();
        }

        private async void BtnSave_Click(object sender, EventArgs e)
        {
            await layoutControl.SaveModelAsync();
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            DialogOk();
        }

        private void editWebCls_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://47.93.100.29/icon");
        }

        private void editWinCls_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (_frmImagePicker == null) _frmImagePicker = new FrmImagePicker();
            if (_frmImagePicker.ShowDialog() == DialogResult.OK)
            {
                if (_frmImagePicker.Options.SelectedItem == null) return;
                picWinCls.Image = _frmImagePicker.Options.SelectedItem.Image;
                layPicWinCls.Visibility = LayoutVisibility.Always;
                editWinCls.Text = _frmImagePicker.Options.SelectedItem.Name;
            }
        }

        private void editWin_CheckedChanged(object sender, EventArgs e)
        {
            layoutWinUrl.Enabled = editWin.Checked;
            layoutWinCls.Enabled = editWin.Checked;
        }

        private void editWeb_CheckedChanged(object sender, EventArgs e)
        {
            layoutWebUrl.Enabled = editWeb.Checked;
            layoutWebCls.Enabled = editWeb.Checked;
        }

        private void layoutControl_BindingForm(object sender, EditFormPanelBindingEventArgs e)
        {
            e.Task = async () =>
            {
                var model = await SysModuleService.Instance.GetOrCreateAsync(e.Args,true);
                await parentModuleTreeList.LoadData(e.Args.Created, model);
                bindingSource.DataSource = model;
                WinFormHelper.SetTreeRootValue(parentModuleTreeList, e.Args.ParentId);

                if (model.WinCls.IsNotEmpty())
                {
                    picWinCls.Image = ImageResourceCache.Default.GetImage(model.WinCls);
                    layPicWinCls.Visibility = LayoutVisibility.Always;
                }
                else
                {
                    layPicWinCls.Visibility = LayoutVisibility.Never;
                }
            };
        }

        private void layoutControl_SaveForm(object sender, EditFormPanelSaveEventArgs e)
        {
            e.Task = async () => await SysModuleService.Instance.SaveAsync(e.Args.Created, (SysModule)e.Model);
        }

        private async void layoutControl_ResetForm(object sender, EditFormPanelResetEventArgs e)
        {
            bindingSource.DataSource = await SysModuleService.Instance.NewMenuAsync(layoutControl.Args.ParentId);
            await layoutControl.BindingModelAsync();
        }
    }
}